package de.baumann.browser.present.gg

import de.baumann.browser.BuildConfig
import de.baumann.browser.api.OdinObserver
import de.baumann.browser.api.net.vo.gg.GGRule
import de.baumann.browser.api.net.vo.gg.Smelter
import de.baumann.browser.base.BasePresenter
import de.baumann.browser.iview.gg.IJoinSmelterView
import de.baumann.browser.model.getPledgeRule
import de.baumann.browser.model.join
import de.baumann.browser.model.rank
import de.baumann.browser.utils.web3j.Erc20Util
import io.reactivex.Observable
import io.reactivex.ObservableOnSubscribe
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import java.lang.Exception

class JoinSmelterPresenter : BasePresenter<IJoinSmelterView>() {

    fun smelteryRank() {
        rank().subscribe(object : OdinObserver<List<Smelter>>(view) {
            override fun onSuccess(data: List<Smelter>?) {
                if (data == null) {
                    return
                }
                view?.setSmelters(data)
            }
        })
    }

    fun joinSmelterTransferToken(fromAddress: String, privateKey: String, smelterAddress: String) {

        Observable.create(ObservableOnSubscribe<String> { emitter ->
            try {
                val hash = Erc20Util.transferERC20Token3(BuildConfig.WALLET_NODE_URL,
                        fromAddress,
                        smelterAddress,
                        view!!.getTime(),
                        privateKey,
                        BuildConfig.GG_ADDRESS,
                        view!!.getQValue())
                if (hash == null) {
                    emitter.onError(Throwable("Transfer Failed"))
                } else {
                    emitter.onNext(hash)
                }
            } catch (e: Exception) {
                emitter.onError(e)
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : Observer<String> {
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable) {
                    }

                    override fun onNext(t: String) {
                        if (fromAddress.startsWith("odx")){
                            joinSmelter("0${fromAddress.substring(2)}", t)
                        }else{
                            joinSmelter(fromAddress, t)
                        }
                    }

                    override fun onError(e: Throwable) {
                        view?.onError(e.message)
                    }
                })
    }


    fun joinSmelter(address: String, hash: String) {
        join(view!!.getId(), address, hash, view!!.getQValue(), view!!.getTime().toString())
                .subscribe(object : OdinObserver<Any>(view) {
                    override fun onSuccess(data: Any?) {
                        view?.joinSuccess()
                    }
                })
    }

    fun getRule() {
        getPledgeRule("2")
                .subscribe(object : OdinObserver<GGRule>(view) {
                    override fun onSuccess(data: GGRule?) {
                        if (data != null) {
                            view?.setRule(data.rule)
                        }
                    }
                })
    }

}